#import "GOPongTable.h"

@implementation GOPongTable

- (id)initWithFrame:(NSRect)frameRect
{
	if ((self = [super initWithFrame:frameRect]) != nil) {
		NSRect r;
		r = [self bounds];
		int y;
		y = (r.size.height/2) - (r.size.height/4)/2;
		upButton1 = @"a";
		downButton1 = @"s";
		upButton2 = @"k";
		downButton2 = @"l";
		
		paddle1 = [[GOPaddle alloc] initWithOrigin:NSMakePoint(20.0, y) andHeight:((r.size.height)/8)];
		paddle2 = [[GOPaddle alloc] initWithOrigin:NSMakePoint((r.size.width -20.0),y) andHeight:((r.size.height)/8)];
		ball = [[GOBall alloc] initWithPosition:NSMakePoint(r.size.width/2, r.size.height/2)];
		[ball setVelocity_x:4.0];
		[ball setVelocity_y:1.0];
		
		[NSTimer scheduledTimerWithTimeInterval:.02
		   target:self 
		   selector:@selector(stepAnimation:) 
		   userInfo:nil 
		   repeats:YES];
	}
	return self;
}

- (void)drawPaddle:(GOPaddle *)paddle
{
	NSBezierPath *path;
	path = [paddle shape];
	[path setLineWidth:4.0];
	[path stroke];
	[path fill];
}

- (void)drawMidLine
{
	NSPoint pointA, pointB;
	NSRect r;
	r = [self bounds];
	pointA = NSMakePoint(r.size.width/2,r.size.height);
	pointB = NSMakePoint(r.size.width/2,0.0);
	NSBezierPath *path;
	path = [[NSBezierPath alloc] init];
	[path moveToPoint:pointA];
	[path lineToPoint:pointB];
	[path closePath];
	[path setLineWidth:1.0];
	[path stroke];
}

- (void)drawBall
{
	[[ball shape] fill];
}

- (void)drawRect:(NSRect)rect
{
	[[NSColor lightGrayColor] set];
	//making the background white
	[NSBezierPath fillRect: [self bounds]];
	[[NSColor blackColor] set];
	[self drawPaddle:paddle1];
	[self drawPaddle:paddle2];
	[self drawBall];
	[self drawMidLine];
}
- (void)movePaddle:(GOPaddle *)paddle
{
	BOOL moveup =((([paddle center].y + [paddle height]) <= [self bounds].size.height) && ([paddle velocity_y] > 0));
	BOOL movedown = (([paddle center].y - [paddle height]) >= 0) && ([paddle velocity_y] < 0);
	if(moveup || movedown)
	{
		paddlemove = [[NSAffineTransform transform] retain];
		[paddlemove translateXBy:0 yBy:[paddle velocity_y]];
		[[paddle shape] transformUsingAffineTransform:paddlemove];
		[paddlemove release];
		paddlemove = [[NSAffineTransform transform] retain];
		[self setNeedsDisplay:YES];
	}

}

- (void)moveBall
{
	ballmove = [[NSAffineTransform transform] retain];
	NSRect box = [self bounds];
	//left side detection
	if([ball center].x < 0)
	{
		[ball setVelocity_x:abs([ball velocity_x])];
		[table scoreOne:2];
	}
	//right side
	if([ball center].x > box.size.width)
	{
		[ball setVelocity_x:(-1 * abs([ball velocity_x]))];
		[table scoreOne:1];
	}
	//top side
	if([ball center].y > box.size.height)
	{
		[ball setVelocity_y:(-1*abs([ball velocity_y]))];
	}
	//bottom side
	if([ball center].y < 0)
	{
		[ball setVelocity_y:abs([ball velocity_y])];
	}
	
	//hitting paddle 1
	if(NSIntersectsRect( [[ball shape] bounds], [[paddle1 shape] bounds] ))
	{
		[ball setVelocity_x:abs([ball velocity_x])];
				NSLog(@"New speed = %f", [ball velocity_x]);
	}
	if(NSIntersectsRect( [[ball shape] bounds], [[paddle2 shape] bounds] ))
	{
		[ball setVelocity_x:(-1*abs([ball velocity_x]))];
		NSLog(@"New speed = %f", [ball velocity_x]);
	}
	
	
	[ballmove translateXBy:[ball velocity_x] yBy:[ball velocity_y]];
	[[ball shape] transformUsingAffineTransform:ballmove];
	[ballmove release];
	[self setNeedsDisplay:YES];
}


- (void)stepAnimation:(NSTimer *)timer
{
	[self movePaddle:paddle1];
	[self movePaddle:paddle2];
	[self moveBall];
}
- (void)keyDown:(NSEvent *)event
{
	BOOL handled = NO;
	NSString *characters;
	//get the pressed key
	characters = [event charactersIgnoringModifiers];
	if([characters isEqualToString:upButton1])
	{
		[paddle1 setVelocity_y:2];
		handled = YES;
	}
	if([characters isEqualToString:downButton1])
	{
		[paddle1 setVelocity_y:-2];
		handled = YES;
	}
	if([characters isEqualToString:upButton2])
	{
		[paddle2 setVelocity_y:2];
		handled = YES;
	}
	if([characters isEqualToString:downButton2])
	{
		[paddle2 setVelocity_y:-2];
		handled = YES;
	}
	if(!handled)
	{
		[super keyDown:event];
	}
}

- (void)keyUp:(NSEvent *)event
{
	BOOL handled = NO;
	NSString *characters;
	//get the released key so we know what player to move a paddle for
	characters = [event charactersIgnoringModifiers];
	// stop the necessary paddle
	if([characters isEqualToString:downButton1] || [characters isEqualToString:upButton1])
	{
		[paddle1 setVelocity_y:0];
		handled = YES;
	}
	if([characters isEqualToString:upButton2] || [characters isEqualToString:downButton2])
	{
		[paddle2 setVelocity_y:0];
		handled = YES;
	}
	if(!handled)
	{
		[super keyUp:event];
	}
}
- (void)awakeFromNib
{
	
}
- (BOOL)acceptsFirstResponder
{
	return YES;
}

@end